package com.pianzhu.blockchainemr;

import com.pianzhu.blockchainemr.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
@EnableAspectJAutoProxy
public class BlockChainEmrApplication implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor tokenInterceptor;

    public static void main(String[] args) {
        SpringApplication.run(BlockChainEmrApplication.class, args);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")         //拦截所有请求
                .excludePathPatterns("/login") //不拦截登录请求
                .excludePathPatterns("/enroll")
                .excludePathPatterns("/hospital/list");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")   //针对什么请求提供跨域支持，此处为支持所有请求
                .allowedOrigins( "http://localhost",
                        "http://localhost:80",
                        "http://localhost:8080",
                        "http://127.0.0.1",
                        "http://127.0.0.1:8080") //允许前端url跨域
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true);				//允许cookie跨域
    }
}
